//
//  DatePickbehavior.m
//  BadBoy
//
//  Created by wen on 2017/3/20.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "DatePickbehavior.h"

@interface DatePickbehavior ()

@property (nonatomic, strong) UIView *blakeView;            //遮盖视图
@property (nonatomic, copy) void(^finishBlock)(NSString *finialDate);

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIDatePicker *pickerView;
@property (nonatomic, strong) UIToolbar *pickerToolBar;
@property (nonatomic, strong) NSDateFormatter* dateFormat;

@end

@implementation DatePickbehavior

- (instancetype)init{
    CGRect frame = [UIScreen mainScreen].bounds;
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.blakeView];
        [self addSubview:self.contentView];
        self.contentView.top = self.height;
        self.blakeView.alpha = 0;
    }
    return self;
}

+ (void)getDateBehaviorByCurrentDate:(NSString *)currentdate startDate:(NSString *)startDate endDate:(NSString *)endDate finishBlock:(void(^)(NSString *finialDate))finishBlock{
    DatePickbehavior *behavior = [[DatePickbehavior alloc] init];
    behavior.finishBlock = finishBlock;
    if ([startDate isNotEmpty]) {
        NSDate *date = [behavior.dateFormat dateFromString:startDate];
        behavior.pickerView.minimumDate = date;
    }
    if ([endDate isEqualToString:datePickNow]) {
        behavior.pickerView.maximumDate = [NSDate date];
    }else if ([endDate isNotEmpty]) {
        NSDate *date = [behavior.dateFormat dateFromString:endDate];
        behavior.pickerView.maximumDate = date;
    }
    if ([currentdate isEqualToString:datePickNow]) {
        [behavior.pickerView setDate:[NSDate date]];
    }else if ([currentdate isNotEmpty]) {
        NSDate *date = [behavior.dateFormat dateFromString:currentdate];
        [behavior.pickerView setDate:date];
    }
    [[UIApplication sharedApplication].keyWindow addSubview:behavior];
    [UIView animateWithDuration:0.3 animations:^{
        behavior.blakeView.alpha = 1;
        behavior.contentView.bottom = kBDScreenHeight;
    } completion:^(BOOL finished) {
        
    }];
}

+ (void)getDateTimeByCurrentDate:(NSString *)currentdate startDate:(NSString *)startDate endDate:(NSString *)endDate finishBlock:(void(^)(NSString *finialDate))finishBlock{
    DatePickbehavior *behavior = [[DatePickbehavior alloc] init];
    behavior.finishBlock = finishBlock;
    behavior.pickerView.datePickerMode = UIDatePickerModeDateAndTime;
    [behavior.dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"];//设定时间格式
    if ([startDate isNotEmpty]) {
        NSDate *date = [behavior.dateFormat dateFromString:startDate];
        behavior.pickerView.minimumDate = date;
    }
    if ([endDate isEqualToString:datePickNow]) {
        behavior.pickerView.maximumDate = [NSDate date];
    }else if ([endDate isNotEmpty]) {
        NSDate *date = [behavior.dateFormat dateFromString:endDate];
        behavior.pickerView.maximumDate = date;
    }
    if ([currentdate isEqualToString:datePickNow]) {
        [behavior.pickerView setDate:[NSDate date]];
    }else if ([currentdate isNotEmpty]) {
        NSDate *date = [behavior.dateFormat dateFromString:currentdate];
        [behavior.pickerView setDate:date];
    }
    [[UIApplication sharedApplication].keyWindow addSubview:behavior];
    [UIView animateWithDuration:0.3 animations:^{
        behavior.blakeView.alpha = 1;
        behavior.contentView.bottom = kBDScreenHeight;
    } completion:^(BOOL finished) {
        
    }];
}

- (UIView *)blakeView{
    if (!_blakeView) {
        self.blakeView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        //给遮盖视图添加点击手势
        UITapGestureRecognizer *tapOpaqueViewGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOpaqueView)];
        [_blakeView addGestureRecognizer:tapOpaqueViewGesture];
        [_blakeView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7]];
    }
    return _blakeView;
}

- (UIDatePicker *)pickerView{
    if (!_pickerView) {
        self.pickerView=[[UIDatePicker alloc]init];
        _pickerView.backgroundColor=[UIColor whiteColor];
        _pickerView.datePickerMode=UIDatePickerModeDate;
        [_pickerView sizeToFit];
//        _pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
        _pickerView.frame=CGRectMake(0, 0, kBDScreenWidth, 216);
        [self addSubview:_pickerView];
    }
    return _pickerView;
}

- (UIToolbar *)pickerToolBar{
    if (!_pickerToolBar) {
        //toolbar
        self.pickerToolBar = [[UIToolbar alloc]  initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 44)]; //创建工具条，用来设置或者退出actionsheet.
        _pickerToolBar.backgroundColor=[UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1.0f];
        [_pickerToolBar sizeToFit];
        
        NSMutableArray *barItems = [[NSMutableArray alloc] init];
        UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        [barItems addObject:flexSpace];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:ASLocalizedString(@"完成") forState:UIControlStateNormal];
        [button setTitleColor:KBDColorBlue forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button addTarget:self action:@selector(closePickView) forControlEvents:UIControlEventTouchUpInside];
        [button sizeToFit];
        button.width += 30;
        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithCustomView:button];
        [barItems addObject:cancelButton];
        [_pickerToolBar setItems:barItems animated:YES]; //将按键加入toolbar
    }
    return _pickerToolBar;
}

- (NSDateFormatter *)dateFormat{
    if (!_dateFormat) {
        self.dateFormat = [[NSDateFormatter alloc] init];
        [_dateFormat setDateFormat:@"yyyy-MM-dd"];//设定时间格式
        NSTimeZone *timeZone = [NSTimeZone localTimeZone];
        [_dateFormat setTimeZone:timeZone];
    }
    return _dateFormat;
}

- (UIView *)contentView{
    if (!_contentView) {
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 0)];
        [_contentView addSubview:self.pickerToolBar];
        [_contentView addSubview:self.pickerView];
        self.pickerView.top = self.pickerToolBar.bottom;
        _contentView.height = self.pickerView.bottom;
    }
    return _contentView;
}

- (void)tapOpaqueView{
    [UIView animateWithDuration:0.3 animations:^{
        self.blakeView.alpha = 0;
        self.contentView.top = kBDScreenHeight;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

-(void)closePickView{
    NSString *dateString = [self.dateFormat stringFromDate:self.pickerView.date];
    // 隐藏
    if (self.finishBlock) {
        self.finishBlock(dateString);
    }
    [self tapOpaqueView];
}


@end
